import java.security.Key;
import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;


public class TestClass {

	/*
	public static void main(String[] argv){
		String userpass = "The quick brown fox jumps over the lazy dog";
		
		//String userpass = findUserPass(clientID);
		
		MessageDigest cript;
		try {
			cript = MessageDigest.getInstance("SHA-1");
			cript.reset();
			cript.update(userpass.getBytes("utf8"));
			byte[] key_20byte = cript.digest();
			//Need to pad out to 24 byte key
//			byte[] secretkey = new byte[24];
//			int n;
//			for(n = 0; n < 20; n++){
//				secretkey[n] = key_20byte[n];
//			}
//			for(;n < 24; n++){
//				secretkey[n] = 'A';
//			}

			byte[] secretkey = new byte[] {'T','h','i','s','I','s','A','S','e','c','r','e','t','K','e','y'};
			String s = "";
			for(int i = 0; i < secretkey.length; i++){
				s += Integer.toString(secretkey[i]);
			}
			
			byte[] dataToSend = "hello world".getBytes();
			
			String valueToEnc = "hello world";
			
			SecretKeySpec key = new SecretKeySpec(secretkey, "AES");
			Cipher c = Cipher.getInstance("AES");
			
			
			c.init(Cipher.ENCRYPT_MODE, key);
			byte[] encValue = c.doFinal(valueToEnc.getBytes());
			String encryptedValue = new BASE64Encoder().encode(encValue);
			
			// Pretend I've sent and received this. Now decrypt
			SecretKeySpec key2 = new SecretKeySpec(secretkey, "AES");
			
			Cipher c2 = Cipher.getInstance("AES");
			c2.init(Cipher.DECRYPT_MODE, key2);
			byte[] decodedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
			byte[] decValue = c.doFinal(decodedValue);
			String decryptedValue = new String(decValue.toString());
			
			for(int i = 0; i < decValue.length; i++){
				System.out.print((char)decValue[i]);
			}
			System.out.println("");
			
			System.out.println(decryptedValue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}*/
	private static final String ALGORITHM = "AES";
	private static final int ITERATIONS = 1;
	
	public static void main (String[] argv){
		String userpass = "mypass";
		String message = "mymessage";
		try {
			MessageDigest cript;
			cript = MessageDigest.getInstance("SHA-1");
			cript.reset();
			cript.update(userpass.getBytes("utf8"));
			byte[] key_20byte = cript.digest();
			//Need to pad out to 24 byte key
			byte[] secretkey = new byte[24];
			int n;
			for(n = 0; n < 20; n++){
				secretkey[n] = key_20byte[n];
			}
			for(;n < 24; n++){
				secretkey[n] = 'A';
			}
			String s = createSessionKey();
			
		//	String passwordEnc = encrypt(message, secretkey);
		//	String passwordDec = decrypt(passwordEnc, secretkey);
			
			String passwordEnc = encrypt(message, new BASE64Decoder().decodeBuffer(s));
			String passwordDec = decrypt(passwordEnc, new BASE64Decoder().decodeBuffer(s));
			
			System.out.println("Plain text: " + message);
			System.out.println("Encrypted: " + passwordEnc);
			System.out.println("Decrypted: " + passwordDec);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static String createSessionKey(){
		Random generator = new Random();
		byte[] res = new byte[32];
		
		//Create random session key, 32 bytes long
		Integer r;
		for(int i = 0; i < res.length; i++ ){	
			r = generator.nextInt(256);
			res[i] = r.byteValue();
		}
		return new BASE64Encoder().encode(res);
	}
	
	private static String encrypt(String msg, byte[] keyValue) throws Exception{
		Key key = new SecretKeySpec(keyValue, ALGORITHM);
		Cipher c = Cipher.getInstance(ALGORITHM);
		c.init(Cipher.ENCRYPT_MODE, key);
		
		String valueToEnc = msg;
		byte[] encValue = c.doFinal(valueToEnc.getBytes());
		String eValue = new BASE64Encoder().encode(encValue);
		return eValue;
	}
	
	private static String decrypt(String msg, byte[] keyValue) throws Exception{
		Key key = new SecretKeySpec(keyValue, ALGORITHM);
		Cipher c = Cipher.getInstance(ALGORITHM);
		c.init(Cipher.DECRYPT_MODE, key);
		
		byte[] decordedValue = new BASE64Decoder().decodeBuffer(msg);
		byte[] decValue = c.doFinal(decordedValue);
		String dValue = new String(decValue);
		return dValue;
	}
	
	private static Key generateKey(byte[] keyValue) throws Exception{
		Key key = new SecretKeySpec(keyValue, ALGORITHM);
		return key;
	}
}
